//
//  RoundImage.m
//  MooreDoll
//
//  Created by 郭景豪 on 2014/12/9.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "RoundImage.h"
@implementation RoundImage

+(UIImage*)circleImage:(UIImage*) image {
    
    CGSize targetSize = CGSizeMake(100, 100);//決定畫布Size
    
    CGRect imageFrame = CGRectMake(0, 0, 100, 100);
    
    UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0);//取得製圖環境，並決定Size 背景為透明，比例不調整
    
    [[UIBezierPath bezierPathWithRoundedRect:imageFrame cornerRadius:100] addClip];//畫出一個圓 設置位置 與角度30 addClip=>裁切
    [image drawInRect:imageFrame];//將圖片放入進去
    
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();//取得製圖裡的圖片
    UIGraphicsEndImageContext();//關閉
    
    return newimg;
}
+ (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
    
    CGImageRef imageRef = image.CGImage;
    CGImageRef maskRef = maskImage.CGImage;
    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                        CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef),
                                        NULL, // decode should be NULL
                                        FALSE // shouldInterpolate
                                        );
    CGImageRef masked = CGImageCreateWithMask(imageRef, mask);
    CGImageRelease(mask);
    UIImage *maskedImage = [UIImage imageWithCGImage:masked];
    CGImageRelease(masked);

    return maskedImage;
}
@end
